/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.util;

import com.eng.scc.core.view.ApplicationContext;
import java.awt.Image;
import java.awt.MediaTracker;
import java.net.URL;
import javax.swing.ImageIcon;

/**
 *
 * @author Rafael
 */
public class ImageFinder
{
    public static Image getImage( String name )
    {
        return getImageFromURL( "images/" + name ).getImage();
    }
    
    public static ImageIcon getImageIcon( String name )
    {
        return getImageFromURL( "images/" + name );
    }
    
    private static ImageIcon getImageFromURL( String path )
    {
        try
        {
            URL url = getURL( path );

            if ( url != null )
            {
                ImageIcon icon = new ImageIcon( url );

                if ( icon != null )
                {
                    if ( icon.getImageLoadStatus() == MediaTracker.COMPLETE )
                    {
                        return icon;
                    }
                }
            }
        }

        catch (Exception e)
        {
//            ApplicationContext.getInstance().logException(e);
        }
        
        return null;
    }
    
    public static URL getURL( String path )
    {
        URL url = null;

        try
        {
            url = ImageFinder.class.getResource( path );

            if ( url == null )
            {
                if ( ! path.startsWith( "/" ) )
                {
                    url = ImageFinder.class.getResource( "/" + path );
                }
            }
        }

        catch (Exception e)
        {
            ApplicationContext.getInstance().saveLog( e );
        }

        return url;
    }
}
